package com.dongdong.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdong.entity.OwnPageInfo;
import com.dongdong.mapper.TbGoodsDescMapper;
import com.dongdong.mapper.TbGoodsMapper;
import com.dongdong.mapper.TbItemMapper;
import com.dongdong.pojo.TbGoods;
import com.dongdong.pojo.TbGoodsDesc;
import com.dongdong.pojo.TbItem;
import com.dongdong.service.GoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Stream;

/**
 * @Deacription TODO
 * @Author Lenovo
 * @Date 2020/12/13 16:33
 * @Version 1.0
 **/
@Service
@Component
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private TbGoodsMapper tbGoodsMapper;

    @Resource
    private TbGoodsDescMapper tbGoodsDescMapper;

    @Resource
    private TbItemMapper tbItemMapper;

    @Override
    public TbGoods findGoodsById(Long goodsId) {
        return tbGoodsMapper.selectByPrimaryKey(goodsId);
    }

    @Override
    public int updateSellerS(Long[] ids, String status) {
        System.out.println(status);
//        Stream.of(ids).forEach(id ->{
//            TbGoods goods = new TbGoods();
//            goods.setId(id);
//            goods.setAuditStatus(status);
//            tbGoodsMapper.updateByPrimaryKeySelective(goods);
//        });
        return tbGoodsMapper.updateSellerS(ids,status);
    }

    @Override
    public OwnPageInfo<TbGoods> SellerCheck(String goodsName, Integer pageNum, Integer pageSize) {
        //分页
        PageHelper.startPage(pageNum,pageSize);
        List<TbGoods> list = tbGoodsMapper.SellerCheck(goodsName);
        PageInfo<TbGoods> pageInfo = new PageInfo(list);
        OwnPageInfo ownPageInfo = new OwnPageInfo();
        ownPageInfo.setPageNum(pageInfo.getPageNum());
        ownPageInfo.setPageSize(pageInfo.getPageSize());
        ownPageInfo.setSize(pageInfo.getSize());
        ownPageInfo.setPages(pageInfo.getPages());
        ownPageInfo.setList(list);
        return ownPageInfo;
    }

    //审核
    @Override
    public void updateStatus(Long[] ids, String status) {
        Stream.of(ids).forEach(id ->{
            TbGoods goods  = new TbGoods();
            goods.setId(id);
            goods.setAuditStatus(status);
            tbGoodsMapper.updateByPrimaryKeySelective(goods);
        });
    }

    @Override
    public List<TbGoods> GoodsCheck(String goodsName, Integer auditStatus, String sellerId) {
        return tbGoodsMapper.GoodsCheck(goodsName,auditStatus,sellerId);
    }

    @Override
    public int insertGoods(TbGoods tbGoods, TbGoodsDesc tbGoodsDesc, String sellerName, List<TbItem> list) {
        //增加goods表 并返回主键
        tbGoodsMapper.insertGoods(tbGoods);
        //增加goods_desc表
        tbGoodsDesc.setGoodsId(tbGoods.getId());
        tbGoodsDescMapper.insertSelective(tbGoodsDesc);
        //增加item表
        for (TbItem tbItem : list) {
            tbItem.setCreateTime(new Date());
            tbItem.setUpdateTime(new Date());
            tbItem.setGoodsId(tbGoods.getId());
            tbItem.setSellerId(tbGoods.getSellerId());
            tbItem.setSeller(sellerName);
            tbItemMapper.insertSelective(tbItem);
        }
        return 1;
    }
}

